package dialogos;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import accesoADatos.AccesoADatos;
import datos.Caja;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CajaDialog extends javax.swing.JDialog {
	private JLabel jFecha;
	private JLabel lSaldoInic;
	private JButton bAceptar;
	public final static SimpleDateFormat formateador = new SimpleDateFormat("dd ' de ' MMMM 'de ' yyyy", new Locale("Es",""));
	private JFormattedTextField tSaldo;

	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				CajaDialog inst = new CajaDialog(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public CajaDialog(JFrame frame) {
		super(frame, "Apertura de Caja", true);
		initGUI();
	}

	private void initGUI() {
		try {
			FlowLayout thisLayout = new FlowLayout();
			thisLayout.setVgap(10);
			thisLayout.setHgap(10);
			getContentPane().setLayout(thisLayout);
			{
				jFecha = new JLabel();
				getContentPane().add(jFecha);
				Date fecha = new Date();
				jFecha.setText("Fecha:  " + formateador.format(fecha));
				jFecha.setPreferredSize(new java.awt.Dimension(220, 21));
				jFecha.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				jFecha.setHorizontalAlignment(SwingConstants.CENTER);
				jFecha.setForeground(new java.awt.Color(0,0,0));
			}
			{
				lSaldoInic = new JLabel();
				getContentPane().add(lSaldoInic);
				lSaldoInic.setText("Saldo inicial");
				lSaldoInic.setPreferredSize(new java.awt.Dimension(102, 21));
				lSaldoInic.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lSaldoInic.setHorizontalAlignment(SwingConstants.CENTER);
				lSaldoInic.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tSaldo = new JFormattedTextField();
				tSaldo.setValue(new Integer(0));
				NumberFormat vFormat = NumberFormat.getCurrencyInstance();
				NumberFormat eFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
				eFormat.setGroupingUsed(false);
				NumberFormatter dvFormat = new NumberFormatter(vFormat);
				NumberFormatter deFormat = new NumberFormatter(eFormat);
				DefaultFormatterFactory fact = new DefaultFormatterFactory(dvFormat,dvFormat,deFormat);
				tSaldo.setFormatterFactory(fact);
				tSaldo.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
				getContentPane().add(tSaldo);
				tSaldo.setPreferredSize(new java.awt.Dimension(105, 21));
				tSaldo.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				tSaldo.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						if(evt.getKeyCode() == KeyEvent.VK_ENTER){
							CajaDialog.this.tSaldo.setFocusable(false);
							CajaDialog.this.tSaldo.setFocusable(true);
						}
					}
				});
			}
			{
				bAceptar = new JButton();
				getContentPane().add(bAceptar);
				bAceptar.setText("Aceptar");
				bAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
				bAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						Date fecha = new Date();
						Caja c = new Caja(fecha);
						c.setSaldoInicial(Double.valueOf(tSaldo.getValue().toString()));//((Integer)CajaDialog.this.tSaldo.getValue()).doubleValue());
						c.setFecha(fecha);
						AccesoADatos.getInstance().setCaja(c);
						CajaDialog.this.dispose();
					}
				});
			}
			pack();
			this.setSize(255, 145);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
